<template>
  <div class="hospital">
    <hos-options @search="search" ref="options"/>
    <hos-table @search="search" :loading="loading" :data="data"/>
    <div class="demonstration-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageParams.size"
        layout="total, prev, pager, next"
        :total="pageParams.totalCount"
      ></el-pagination>
     </div>
  </div>
</template>

<script>
import HosOptions from './controls/hosOptions'
import HosTable from './controls/hosTable'
import { hosLevelList } from "data/listBody"
import queryString from "querystring"
import page from 'data/page'

export default {
  name: 'hospital',
  mixins: [page],
  data() {
    return {
      data:[], //table数据
      currentPage: 1,
      loading: false
    }
  },
  components: {
    HosOptions,
    HosTable
  },
  mounted(){
    this.search();
  },
  methods:{
    search(){  //搜索
      this.loading = true;
      const optionParams = this.$refs.options.getData();
      const tableParams = {
        page: this.currentPage - 1,
        size: 20
      };
      let params = this.lodash.assign({}, optionParams, tableParams);
      const param = queryString.stringify(params || {});
      this.api.get(`/hospital?${param}`).then((res)=>{
        this.loading = false;
        if(!res.data) return;
        this.data = res.data.list;
        this.setPage(res.data)
        this.dataInit();
      }).catch((err) => {
        console.log(err)
        this.$message(err.message)
      })
    },
    dataInit(){ //数据初始化
      this.data.map((item) => {
        let hosArr = hosLevelList.filter(obj => {
          return obj.dKey == item.level;
        })
        if(hosArr.length > 0) return item.levelName = hosArr[0].dValue;       
        return item
      })
    }
  },
  watch: {
    currentPage() {
      this.search()
    }
  }
}
</script>

<style lang="less" scoped>

</style>
